<?php

namespace App\Containers\AppSection\Profile\Models;

use App\Containers\AppSection\Accreditation\Models\Accreditation;
use App\Containers\AppSection\Ban\Models\Ban;
use App\Containers\AppSection\Ban\Traits\HasBansModel;
use App\Containers\AppSection\BankDetail\Models\BankDetail;
use App\Containers\AppSection\File\Models\File;
use App\Containers\AppSection\LegalForm\Models\LegalForm;
use App\Containers\AppSection\Project\Models\Project;
use App\Containers\AppSection\Qualification\Models\Qualification;
use App\Containers\AppSection\Status\Models\Status;
use App\Containers\AppSection\User\Models\User;
use App\Ship\Parents\Models\Model;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
 * Профиль
 *
 * @property int $id Id
 * @property string $user_id Пользователь
 * @property string $phone Номер
 * @property string $phone_verified_at Время подтверждения номера
 * @property string $role_key Роль
 * @property string $ban_id Бан
 * @property string $legal_form_type Тип правовой формы
 * @property int $legal_form_id Id правовой формы
 * @property string $avatar_id Аватар
 * @property string $accreditation_status_key Статус аккредитации
 * @property int $accreditation_id Аккредитация
 * @property string $qualification_status_key Статус квалификации
 * @property int $qualification_id Квалифкация
 * @property Carbon $accredited_at Время Аккредитации
 * @property Carbon $created_at Время создания
 * @property Carbon $updated_at Время обновления
 *
 * @property User $user Владелец
 * @property LegalForm $legal_form Правовая форма
 * @property Status $accreditation_status Статус аккредитации
 * @property File $avatar Аватар
 * @property BankDetail[] $bank_details Банковские реквизиты
 * @property Accreditation $accreditation Аккредитация
 * @property Accreditation[] $accreditations Аккредитации
 * @property Project[] $projects Проекты
 * @property Status $qualification_status Статус квалификации
 * @property Qualification $qualification Квалифкация
 * @property Qualification[] $qualifications Квалифкации
 */
class Profile extends Model
{
    use HasBansModel;

    protected $fillable = [
        'user_id',
        'phone',
        'phone_verified_at',
        'accreditation_status_key',
        'accreditation_id',
        'avatar_id',
        'legal_form_type',
        'legal_form_id',
        'qualification_status_key',
        'qualification_id',
        'accredited_at',
        'ban_id'
    ];

    protected $casts = [

    ];

    protected $dates = [
        'created_at',
        'updated_at',
        'phone_verified_at',
        'accredited_at',
    ];


    /**
     * A resource key to be used in the serialized responses.
     */
    protected string $resourceKey = 'Profile';

    protected string $banKey = 'user_id';

    /**
     * Владелец
     * @return BelongsTo User
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id', 'id')->withTrashed();
    }

    /**
     * Статус аккредитации
     * @return BelongsTo Status
     */
    public function accreditation_status(): BelongsTo
    {
        return $this->belongsTo(Status::class, 'accreditation_status_key', 'key');
    }

    /**
     * Правовая форма
     * @return BelongsTo LegalForm
     */
    public function legal_form(): BelongsTo
    {
        return $this->belongsTo(LegalForm::class, 'legal_form_id', 'id');
    }

    /**
     * Подтверждение номера телефона
     * @return bool
     */
    public function EnsurePhoneIsVerified(): bool
    {
        return !!$this->getAttribute('phone_verified_at');
    }

    /**
     * Банковские реквизиты
     * @return HasMany|BankDetail BankDetails
     */
    public function bank_details(): HasMany
    {
        return $this->hasMany(BankDetail::class, 'profile_id', 'id');
    }

    /**
     * Аватар
     * @return BelongsTo File
     */
    public function avatar(): BelongsTo
    {
        return $this->belongsTo(File::class, 'avatar_id', 'id');
    }

    /**
     * Аккредитация
     * @return BelongsTo Accreditation
     */
    public function accreditation(): BelongsTo
    {
        return $this->belongsTo(Accreditation::class, 'accreditation_id', 'id');
    }

    /**
     * Аккредитации
     * @return hasMany Accreditation
     */
    public function accreditations(): hasMany
    {
        return $this->hasMany(Accreditation::class, 'profile_id', 'id');
    }

    /**
     * Проекты
     * @return HasMany|Project Projects
     */
    public function projects(): HasMany
    {
        return $this->hasMany(Project::class, 'profile_id', 'id');
    }

    /**
     * @param $legalForm
     * @return bool
     */
    public function hasLegalForm($legalForm): bool
    {
        return $this->legal_form_type === $legalForm;
    }

    /**
     * @param $status
     * @return bool
     */
    public function hasAccreditationStatus($status): bool
    {
        return $this->accreditation_status_key === $status;
    }

    /**
     * @param $status
     * @return bool
     */
    public function hasQualificationStatus($status): bool
    {
        return $this->qualification_status_key === $status;
    }

    /**
     * Квалификация
     * @return BelongsTo
     */
    public function qualification(): BelongsTo
    {
        return $this->belongsTo(Qualification::class, 'qualification_id', 'id');
    }

    /**
     * Квалификации
     * @return hasMany
     */
    public function qualifications(): hasMany
    {
        return $this->hasMany(Qualification::class, 'profile_id', 'id');
    }

    /**
     * Статус квалификации
     * @return BelongsTo
     */
    public function qualification_status(): BelongsTo
    {
        return $this->belongsTo(Status::class, 'qualification_status_key', 'key');
    }
}
